'use strict';

const { Controller } = require('ee-core');
const { dialog } = require('electron');
const _ = require('lodash');
const CoreWindow = require('ee-core/electron/window');

/**
 * 特效 - 功能demo
 * @class
 */
class EffectController extends Controller {

  constructor(ctx) {
    super(ctx);
  }

  /**
   * 选择文件
   */
  // selectFile() {
  //   const filePaths = dialog.showOpenDialogSync({
  //     properties: ['openFile']
  //   });

  //   if (_.isEmpty(filePaths)) {
  //     return null
  //   }

  //   return filePaths[0];
  // }


   selectFile(args) {
     // 根据type参数设置文件类型过滤器
     const {type,limit} = args
    let filters = [];
    if (type === 'all') {
      filters = [{ name: 'All Files', extensions: ['*'] }];
    } else if (typeof type === 'string') {
      filters = [{ name: type, extensions: [type] }];
    } else if (Array.isArray(type)) {
      filters = type.map(ext => ({ name: ext.toUpperCase(), extensions: [ext] }));
    }
  
    // 设置是否多选
    let properties = ['openFile'];
    if (limit === 'multiple') {
      properties.push('multiSelections');
    }
  
    // 显示文件选择对话框
    const filePaths = dialog.showOpenDialogSync({
      properties: properties,
      filters: filters
    });
  
    if (!filePaths || filePaths.length === 0) {
      return null;
    }
  
    return limit === 'multiple' ? filePaths : filePaths[0];
  }
  
  /**
   * login window
   */
  loginWindow(args) {
    const { width, height } = args;
    const win = CoreWindow.getMainWindow();

    const size = {
      width: width || 400,
      height: height || 300
    }
    win.setSize(size.width, size.height);
    win.setResizable(true);
    win.center();
    win.show();
    win.focus();
  }

  /**
   * restore window
   */
  restoreWindow(args) {
    const { width, height } = args;
    const win = CoreWindow.getMainWindow();

    const size = {
      width: width || 980,
      height: height || 650
    }
    win.setSize(size.width, size.height);
    win.setResizable(true);
    win.center();
    win.show();
    win.focus();
  }
}

EffectController.toString = () => '[class EffectController]';
module.exports = EffectController;  